<?php
/**
 * 管理员后台控制器
 * 
 * @version $Id: Report.php 633 2013-01-05 04:50:22Z linsir123 $
 */
class controllerReport extends AdminController 
{
	/**
	 * @var 模型
	 */
	public $mReport;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$this->mReport = MA::report();
	}
	
	/**
	 * 登录并且获取授权
	 */
	public function actionIndex()
	{
		if (Request::isPost()) {
			$conf = App::O('taobaoke.app');
			$appkey = $data[0];
			
			///
			$startDate = trim($_POST['start_date']);
			$endDate = trim($_POST['end_date']);
			if (lpString::isDate($startDate) && lpString::isDate($endDate)) {
				$startDate = strtotime($startDate);
				$endDate = strtotime($endDate);
				if ($startDate < $endDate) {
					
					/* */
					while ($startDate <= $endDate) {
						$content = '----------------------------------['. 
							date('Y-m-d', $startDate) 
							.']----------------------------------\r\n';
						$this->_echo($content);
						
						///
						$date = date('Ymd', $startDate);
						$startDate += 3600*24;
						$list = $this->mReport->getByDate($date);
						foreach ($list as $v) {
							$content = round($v['commission'], 2) .' || '. 
								round($v['pay_price'], 2) .' || '. 
								$v['item_title'] . '\r\n';
								
							///
							$this->_echo($content);
						}
					}
					
				} else 
					$this->tipMsg('开始时间不能大于结束时间', 1, 0, 'report');
					
			} else 
				$this->tipMsg('日期格式有误', 1, 0, 'report');
				
		} else {
			View::display('report/index');
			View::assignSet(array(
				'title' => '淘宝报表',
				'date' => array(
					'start'=>date('Y-m-d', strtotime("-5 day")),
					'end'=>date('Y-m-d')
				)
			));
		}
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 输出
	 */
	private function _echo($message)
	{
		echo '<script type="text/javascript">parent.displayResult("', $message, '");</script><br />';
		
		///
		ob_flush();
		flush();
	}
}
